//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "quadratic.h"
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Cquadratic::Cquadratic (Double a, Double b, Double c) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phquadratic = NULL;
#endif
        QuantLib::Real _a = (QuantLib::Real)ValueHelper::Convert (a);
        QuantLib::Real _b = (QuantLib::Real)ValueHelper::Convert (b);
        QuantLib::Real _c = (QuantLib::Real)ValueHelper::Convert (c);
        _ppquadratic = new boost::shared_ptr<QuantLib::quadratic> (new QuantLib::quadratic ( _a,  _b,  _c ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Cquadratic::Cquadratic (boost::shared_ptr<QuantLib::quadratic>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phquadratic = NULL;
#endif
	_ppquadratic = &childNative;
    
}
Cephei::QL::Math::Cquadratic::Cquadratic (QuantLib::quadratic& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phquadratic = NULL;
#endif
	_ppquadratic = new boost::shared_ptr<QuantLib::quadratic> (&childNative);
    
    _quadraticOwner = owner;
    
}

Cephei::QL::Math::Cquadratic::Cquadratic (Cquadratic^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phquadratic = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppquadratic = new boost::shared_ptr<QuantLib::quadratic> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Cquadratic::Cquadratic (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phquadratic = NULL;
#endif
	if (!t->IsSubclassOf(Cquadratic::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Cquadratic::Cquadratic (QuantLib::Handle<QuantLib::quadratic>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phquadratic = &childNative;
	_ppquadratic = &static_cast<boost::shared_ptr<QuantLib::quadratic>>(childNative.currentLink());
    
    _quadraticOwner = owner;
}
Cephei::QL::Math::Cquadratic::Cquadratic (QuantLib::Handle<QuantLib::quadratic> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phquadratic = &childNative;
	_ppquadratic = &static_cast<boost::shared_ptr<QuantLib::quadratic>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Cquadratic::Cquadratic (QuantLib::quadratic childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phquadratic = NULL;
#endif
	_ppquadratic = new boost::shared_ptr<QuantLib::quadratic> (new QuantLib::quadratic (childNative));
    
}
#endif

Cephei::QL::Math::Cquadratic::~Cquadratic ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppquadratic != NULL)
    {
	    delete _ppquadratic;
        _ppquadratic = NULL;
    }
}
Cephei::QL::Math::Cquadratic::!Cquadratic ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppquadratic != NULL)
    {
	    delete _ppquadratic;
    }
}
QuantLib::quadratic& Cephei::QL::Math::Cquadratic::GetReference ()
{
    if (_ppquadratic == NULL) throw gcnew NativeNullException ();
	return **_ppquadratic;
}
boost::shared_ptr<QuantLib::quadratic>& Cephei::QL::Math::Cquadratic::GetShared ()
{
    if (_ppquadratic == NULL) throw gcnew NativeNullException ();
	return *_ppquadratic;
}
QuantLib::quadratic* Cephei::QL::Math::Cquadratic::GetPointer ()
{
    if (_ppquadratic == NULL) throw gcnew NativeNullException ();
	return &**_ppquadratic;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::quadratic>& Cephei::QL::Math::Cquadratic::GetHandle ()
{
	if (_phquadratic == NULL)
	{
		_phquadratic = new Handle<QuantLib::quadratic> (*_ppquadratic);
	}
	return *_phquadratic;
}
#endif
bool Cephei::QL::Math::Cquadratic::HasNative () 
{
	return (_ppquadratic != NULL);
}

Double Cephei::QL::Math::Cquadratic::Discriminant::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppquadratic)->discriminant ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Math::Cquadratic::Roots (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y);
    	bool _rv = (bool)(*_ppquadratic)->roots ( _x,  _y );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Cquadratic::TurningPoint::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppquadratic)->turningPoint ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Cquadratic::ValueAtTurningPoint::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppquadratic)->valueAtTurningPoint ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Iquadratic^ Cephei::QL::Math::Cquadratic_Factory::Create (Double a, Double b, Double c)
{
    return gcnew Cquadratic ( a,  b,  c);
}
